package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xgkj.diyiketang.ProtocolConst;
import com.xgkj.diyiketang.activity.data.YueChangeAdapter;
import com.xgkj.diyiketang.data.OrderInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.MyYueBianHuaTask;
import com.xgkj.diyiketang.util.NoDataUtil;
import com.xgkj.diyiketang.weidget.PullToRefreshListView;
import com.xgkj.diyiketang.R;

import java.util.ArrayList;
import java.util.List;

public class ShouzhimingxiActivity extends BaseActivity implements PullToRefreshListView.OnLoadDataListener,PullToRefreshListView.OnRefreshListener{
    private Context mContext;
    private final String TAG = getClass().getSimpleName();
    private PullToRefreshListView mListView;
    private YueChangeAdapter mAdapter;
    private int currentPage = 1;
    private int size = 10;
    private int mYueChangeTaskId = -1;
    private NoDataUtil mNoData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shouzhimingxi);
        mContext = this;
        initView();
    }

    private void initView() {
        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        View v = findViewById(R.id.content_layout);
        mNoData = new NoDataUtil(mContext,v);
        mListView = (PullToRefreshListView)findViewById(R.id.yue_list);
        mListView.init(mContext,true);
        mAdapter = new YueChangeAdapter(mContext);
        mListView.setAdapter(mAdapter, true);
        mListView.setOnRefreshListener(this);
        mListView.setOnLoadDataListener(this);
        mListView.doRefresh();
    }

    private void getYueChangeList(){
        if(mYueChangeTaskId != -1){
            return;
        }
        if(currentPage == 0){
            mListView.setNoNextPage();
            return;
        }

        MyYueBianHuaTask stask = new MyYueBianHuaTask(size,currentPage);
        stask.addListener(changelistener);
        mYueChangeTaskId = getEatShowApp().getTaskManager().addTask(stask);

    }

    private TaskEventListener changelistener  = new ShowToastTaskEventListener(this){
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if(taskId == mYueChangeTaskId){
                mYueChangeTaskId = -1;
                if(currentPage == 1){
                    mAdapter.clearData();
                    mListView.onRefreshComplete();
                    mListView.enableLoadMore();
                }else{
                    mListView.onLoadDataComplete(true);
                }
                List<OrderInfo> mfoodlist = new ArrayList<OrderInfo>();
                if(data != null){
                    int pageCount = 0;
                    int currentPage = 0;
                    int nextPage = 0;

                    JSONObject jsonString = (JSONObject) data;


                        currentPage = jsonString.getIntValue(ProtocolConst.CURRENT_PAGE);
                        pageCount = jsonString.getIntValue(ProtocolConst.PAGE_COUNT);
                        nextPage = currentPage >= pageCount ? 0 : (currentPage + 1);
                        mfoodlist = JSON.parseArray(jsonString.getString(ProtocolConst.LIST_TAG), OrderInfo.class);

                }

                if(mfoodlist != null && mfoodlist.size()>0){
                    mAdapter.updateData(mfoodlist);
                }

                if (mAdapter != null && mAdapter.getCount() ==0){
                    mNoData.ListViewEmpty(mListView,null,null,-1);
                }else {
                    mNoData.ListViewNoEmpty(mListView);
                }

                currentPage = result;


            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
            if(taskId == mYueChangeTaskId){
                mYueChangeTaskId = -1;
                if(currentPage == 1){
                    mListView.onRefreshComplete();
                }else{
                    mListView.onLoadDataComplete(false);
                }
            }
        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {
            super.handleTaskMessage(taskId, data);
        }
    };

    @Override
    public void onPullRefresh() {

    }

    @Override
    public void onLoadMore() {
        getYueChangeList();
    }

    @Override
    public void onRefresh() {
        currentPage = 1;
        getYueChangeList();
    }
}
